package com.qixin.modules.ea.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qixin.modules.ea.service.IEaFpService;
import com.qixin.modules.eaBasic.vo.IdVo;
import com.qixin.modules.rzInvoice.entity.*;
import com.qixin.modules.rzInvoice.mapper.EaBillMapper;
import com.qixin.modules.rzInvoice.vo.EaBillPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author:wly
 * @Date:2021/9/9
 * @Describtion: app发票信息
 */
@Api(tags="app发票信息")
@RestController
@RequestMapping("/app/api/eaFp")
@Slf4j
public class ApiEaFpController {
    @Autowired
    private IEaFpService iEaFpService;
    @Autowired
    private EaBillMapper eaBillMapper;

    /**
     * 添加发票信息
     *
     * @param eaBillPage
     * @return
     */
    @AutoLog(value = "发票信息-添加")
    @ApiOperation(value="发票信息-添加", notes="发票信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody EaBillPage eaBillPage) {
        Map<String,Object> result = iEaFpService.saveMain(eaBillPage);
        if(result.get("code").equals("0")){  //成功
            return Result.OK(result);
        }else{
            return Result.error(result.get("message").toString());
        }
    }

    /**
     * 查询发票信息列表
     *
     * @param map,request
     * @return Result
     */
    @ApiOperation(value="发票信息列表-查询", notes="发票信息列表-查询")
    @PostMapping(value = "/list")
    public Result<?> list(@RequestBody  Map<String,Object> map,HttpServletRequest request) {
        Map<String,Object> resutlList = iEaFpService.list(map);
        return Result.OK(resutlList);
    }

    /**
     * 查询发票详情
     *
     * @param idVo
     * @return
     */
    @ApiOperation(value="发票详情-查询", notes="发票详情-查询")
    @PostMapping(value = "/selectById")
    public Result<?> selectById(@RequestBody IdVo idVo, HttpServletRequest request) {
        String id = idVo.getId();
        EaBill eaBill = eaBillMapper.selectById(id);
        if(eaBill==null) {
            return Result.error("未找到对应数据");
        }
        Map<String, Object> resutlList = iEaFpService.selectById(id);
        return Result.OK(resutlList);
    }

    /**
     * 编辑发票信息
     *
     * @param eaBillPage
     * @return
     */
    @AutoLog(value = "发票信息-编辑")
    @ApiOperation(value="发票信息-编辑", notes="发票信息-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody EaBillPage eaBillPage) {
        EaBill eaBill = eaBillMapper.selectById(eaBillPage.getId());
        if(eaBill==null) {
            return Result.error("未找到对应数据");
        }
        // update-begin-by wujinbiao 2022/3/31 10:36 for 判断发票是否被引用，被引用不可编辑
        if(StringUtils.isNotBlank(eaBill.getIsused()) && eaBill.getIsused().equals("1") ){
            return Result.error("当前发票已被引用不可编辑");
        }
        // update-end-by wujinbiao 2022/3/31 10:36 for 判断发票是否被引用，被引用不可编辑
        Map<String,Object> returnMap = iEaFpService.updateMain(eaBillPage);
        return Result.OK(returnMap);
    }

    /**
     * 通过id删除发票
     *
     * @param idVo
     * @return
     */
    @AutoLog(value = "发票信息-通过id删除")
    @ApiOperation(value="发票信息-通过id删除", notes="发票信息-通过id删除")
    @PostMapping(value = "/delete")
    public Result<?> delete(@RequestBody IdVo idVo) {
        String id = idVo.getId();
        // update-begin-by wujinbiao 2022/3/31 10:35 for 判断发票是否被引用，被引用不可删除
        EaBill eaBill = eaBillMapper.selectById(id);
        if(StringUtils.isNotBlank(eaBill.getIsused()) && eaBill.getIsused().equals("1") ){
            return Result.error("当前发票已被引用不可删除");
        }
        // update-end-by wujinbiao 2022/3/31 10:35 for 判断发票是否被引用，被引用不可删除
        Map<String,Object> returnMap = iEaFpService.delMain(id);
        if(returnMap.get("code").equals("0")){
            return Result.OK("删除成功!");
        }else{
            String message = returnMap.get("message") != null ? returnMap.get("message").toString() : "";
            return Result.error(message);
        }
    }

}
